package moe.oil.db.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import moe.oil.dto.school.SchoolDTO;
import moe.oil.entity.BaseEntity;
import moe.oil.enumeration.SchoolType;
import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;

import javax.persistence.*;
import java.util.List;

@Table
@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
public class School implements BaseEntity<SchoolDTO> {
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "school_seq")
    Long schoolId;

    @Column(name = "school_name")
    String schoolName;

    @Column(name = "school_type")
    SchoolType schoolType;

    @Column(name = "school_icon_url")
    String schoolIconUrl;

    @Column(name = "address")
    String address;

    @Column(name = "longitude")
    String longitude;

    @Column(name = "latitude")
    String latitude;

    //todo 环境图片多个
    @Column(name = "environment_url")
    String environmentUrl;


    @JoinColumn
    // cascade 参数表示级联操作 Persist：保存 Remove：删除  Refresh：刷新 Merge：合并
    @OneToMany(cascade = {CascadeType.ALL})
    @LazyCollection(LazyCollectionOption.FALSE)
    List<Comments> comments;


    @JoinColumn
    @OneToMany(cascade = CascadeType.ALL)
    @LazyCollection(LazyCollectionOption.FALSE)
    List<User> users;

    @Override
    public String toString() {
        return String.format(
                "School{schoolId='%s', schoolName='%s', schoolType='%s', schoolIconUrl='%s', address='%s', longitude='%s', latitude='%s', environmentUrl='%s' , users='%s'}",
                schoolId, schoolName, schoolType, schoolIconUrl, address, longitude, latitude, environmentUrl, users);
    }

    public School(SchoolDTO schoolDTO) {
        this.schoolId = schoolDTO.getSchoolId();
        this.schoolName = schoolDTO.getSchoolName();
        this.schoolType = schoolDTO.getSchoolType();
        this.schoolIconUrl = schoolDTO.getSchoolIconUrl();
        this.address = schoolDTO.getAddress();
        this.longitude = schoolDTO.getLongitude();
        this.latitude = schoolDTO.getLatitude();
        this.environmentUrl = schoolDTO.getEnvironmentUrl();
    }

    @Override
    public SchoolDTO convert() {
        return new SchoolDTO()
                .setAddress(address)
                .setEnvironmentUrl(environmentUrl)
                .setLatitude(latitude)
                .setLongitude(longitude)
                .setSchoolIconUrl(schoolIconUrl)
                .setSchoolId(schoolId)
                .setSchoolName(schoolName)
                .setSchoolType(schoolType);

    }
}
